﻿Imports SIGI.Common.BE
Imports SIGI.Common

Public Class frmRecoverPassword
#Region "<< -Attributes- >>"
    Private _User As UserBE = Nothing
    Private _Tries As Integer = 0
    Private _MaxTries As Integer = 0
    Private _UserController As UsersController = New UsersController
#End Region

#Region "<< -Events- >>"
    Private Sub frmResetPassword_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim wAppConfig As AppConfigController = Nothing
        Dim wMaxTries As AppConfigBE = Nothing

        Try
            wAppConfig = New AppConfigController
            wMaxTries = wAppConfig.SearchById(CommonDefinitions.AppConfig_BlockingMaxTryKey)
            If Not wMaxTries Is Nothing Then
                'Valido que la propiedad traiga un valor númerico entero
                If Integer.TryParse(wMaxTries.Value, _MaxTries) = 0 Then
                    _MsgBox.ShowMessage("Clave Cantidad Intentos Bloqueo", "Propiedad mal configurada. El valor debe ser númerico",
                                        frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
                    btnAcept.Enabled = False
                    btnCancel.Enabled = False
                End If
            Else
                _MsgBox.ShowMessage("Clave Cantidad Intentos Bloqueo", "Propiedad Inexistente. El valor debe ser númerico",
                                        frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
            End If
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        Try
            If String.IsNullOrEmpty(txtUser.Text.Trim) Then Return
            _User = _UserController.SearchByUserName(txtUser.Text.Trim)
            If _User Is Nothing Then
                _MsgBox.ShowMessage("Usuario Inexistente", "El Usuario Ingresado no existe", frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
                Return
            End If

            txtSecretQuestion.Text = _User.Question.Description
            txtAnswer.Focus()
            txtAnswer.SelectAll()
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        txtUser.Text = String.Empty
        txtSecretQuestion.Text = String.Empty
        txtAnswer.Text = String.Empty
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnAcept_Click(sender As System.Object, e As System.EventArgs) Handles btnAcept.Click
        Try
            If txtAnswer.Text.Trim.ToLower() = _User.Answer.ToLower() Then
                txtPassword.Text = _User.DecryptPassword() 'La contraseña se encuentra encriptada originalmente
            Else
                _MsgBox.ShowMessage("Respuesta Incorrecta", "Respuesta incorrecta", frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
                _Tries += 1
                lblNTries.Text = String.Format("Nº de Intentos: {0}", _Tries.ToString())
                If _Tries >= _MaxTries Then 'Fallo el màximo de intetos permitidos, el usuario se bloquea.
                    'Bloquea al usuario
                    _UserController.BlockUser(_User.UserName, True)
                    _MsgBox.ShowMessage("Usuario Bloqueado", "Ingreso Bloqueado – Consulte con Administrador", frmMessage.MessageType.Information_, frmMessage.MessageButtons.Ok)
                    Me.Close()
                End If
                txtAnswer.Focus()
                txtAnswer.SelectAll()
            End If
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub
#End Region

End Class
